<template>
    <div class="app-container">
        <el-card class="operate-container" shadow="never" style="margin-bottom: 20px;">
            <i class="el-icon-tickets" style="margin-top: 5px"></i>
            <span style="margin-top: 5px">数据列表</span>
            <el-button class="btn-add" @click="addProductAttrCate()" size="mini">添加</el-button>
        </el-card>

        <div class="table-container">
            <el-table ref="productAttrCateTable" style="width: 100%" :data="list" border>
                <el-table-column prop="id" label="编号" width="180"></el-table-column>
                <el-table-column prop="name" label="类型名称" width="180"></el-table-column>
                <el-table-column prop="attributeCount" label="属性数量"></el-table-column>
                <el-table-column prop="paramCount" label="参数数量"></el-table-column>
                <el-table-column label="设置" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="getAttrList(scope.$index, scope.row)">属性列表</el-button>
                        <el-button size="mini" @click="getParamList(scope.$index, scope.row)">参数列表</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="pagination-container" style="margin-top: 10px;">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes,prev, pager, next,jumper" 
            :current-page.sync="listQuery.pageNum" :page-size="listQuery.pageSize" :total="total" :page-sizes="[2,,5,10,15]">
            </el-pagination>
        </div>

        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" :before-close="handleClose()" width="30%">
            <el-form ref="productAttrCatForm" :model="productAttrCate" :rules="rules" label-width="120px">
                <el-form-item label="类型名称" prop="name">
                    <el-input v-model="productAttrCate.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleConfirm('productAttrCatForm')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {queryProductAttrCatList, addProductAttrCat, updateProductAttrCat, deleteProductAttrCatById} from '@/api/pms/productAttr'

export default {
    name: 'productAttrCateList',
    data() {
        return {
            list: [],
            total: null,
            listQuery: {
                pageNum: 1,
                pageSize: 5
            },
            dialogTitle: '',
            dialogVisible: false,
            productAttrCate: {
                name: '',
                id: null
            },
            rules: {

            }
        }
        
    },
    created() {
        this.getList();
    },
    methods: {
        // 获取属性分类列表
        getList() {
            queryProductAttrCatList(this.listQuery).then(response => {
                let data = response.data.data;
                this.list = data.list;
                this.total = data.total;
            });
        },
        // 属性列表
        getAttrList(index, row) {

        },
        // 参数列表
        getParamList(index, row) {

        },
        // 编辑
        handleUpdate(index, row) {
            this.dialogVisible = true;
            this.dialogTitle = "编辑类型";
            this.productAttrCate.name = row.name;
            this.productAttrCate.id = row.id;
        },
        // 删除
        handleDelete(index, row) {
            this.$confirm('是否要删除该品牌', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteProductAttrCatById(row.id, {}).then(response => {
                    this.$message({
                        message: '删除成功',
                        type: 'success',
                        duration:1000
                    });
                    this.getList();
                })
            });
        },
        // 每页个数变更
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        // 页数变更
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        // 添加属性分类
        addProductAttrCate() {
            this.dialogVisible = true;
            this.dialogTitle = "添加类型";
        },
        // 取消
        handleClose() {
            if(!this.dialogVisible && this.$refs.productAttrCatForm) {
                this.$refs.productAttrCatForm.clearValidate()
            }
        },
        // 确认
        handleConfirm(formName) {
            this.$refs[formName].validate((valid) =>{
                if (valid) {
                    let data = new URLSearchParams();
                    data.append("name",this.productAttrCate.name);
                    if(this.dialogTitle === "添加类型") {
                        addProductAttrCat(data).then(response => {
                            this.$message({
                                message: '添加成功',
                                type: 'success',
                                duration:1000
                            });
                            this.dialogVisible = false;
                            this.getList();
                        })
                    } else {
                        updateProductAttrCat(this.productAttrCate.id, data).then(response => {
                            this.$message({
                                message: '修改成功',
                                type: 'success',
                                duration:1000
                            });
                            this.dialogVisible = false;
                            this.getList();
                        })
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
        }
    }
}
</script>

<style>

</style>